package com.innovativethinking.domain;


import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.users.User;



@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class KanbanTweet {
	
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;

	@Persistent
    private String tweet;
	
	@Persistent
    private String buffer;
	
	@Persistent
    private Date registerDate;
	
	@Persistent
    private Date doneDate;
	
	@Persistent
    private User owner;
	
	public KanbanTweet(String tweet, User owner) {
		
		if (tweet.length() > 500) {
			this.tweet = tweet.substring(0, 500);
		} else {
			this.tweet = tweet;
		}
		
		this.owner = owner;
		this.buffer = BufferName.INPUTQUEUE;
		this.registerDate = new Date();
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getTweet() {
		return tweet;
	}

	public void setTweet(String tweet) {
		this.tweet = tweet;
	}

	public Date getRegisterDate() {
		return registerDate;
	}

	public void setRegisterDate(Date registerDate) {
		this.registerDate = registerDate;
	}

	public Date getDoneDate() {
		return doneDate;
	}

	public void setDoneDate(Date doneDate) {
		this.doneDate = doneDate;
	}

	public User getOwner() {
		return owner;
	}

	public void setOwner(User owner) {
		this.owner = owner;
	}
	
	public String getBuffer() {
		return buffer;
	}

	public void setBuffer(String buffer) {
		this.buffer = buffer;
	}

	public String getElapsedTime() {
		long since = registerDate.getTime();
		
		long now = System.currentTimeMillis();
		
		long diff = now - since;
		
		float elapsedTimeSec = diff/1000F;
		
		if (elapsedTimeSec < 1.0f) return "less than 1 second";
		
		if (elapsedTimeSec < 60.0f) return new String(new Float(elapsedTimeSec).intValue() + " seconds ago");
		
		float elapsedTimeMin = diff/(60*1000F);
		
		if (elapsedTimeMin < 2.0f) return new String("about " + new Float(elapsedTimeMin).intValue() + " minute ago");
		
		if (elapsedTimeMin < 59.9f) return new String(new Float(elapsedTimeMin).intValue() + " minutes ago");
		
		float elapsedTimeHour = diff/(60*60*1000F);
		
		if (elapsedTimeHour < 2.0f) return new String("about " + new Float(elapsedTimeHour).intValue() + " hour ago");
		
		if (elapsedTimeHour < 24.0f) return new String(new Float(elapsedTimeHour).intValue() + " hours ago");
		
		return new String(new Float(diff/(24*60*60*1000F)).intValue() + " days ago");	
		
	}
	
	public String getCycleTime() {
		
		long since = registerDate.getTime();
		
		long now = doneDate.getTime();
		
		long diff = now - since;
		
		float elapsedTimeSec = diff/1000F;
		
		if (elapsedTimeSec < 1.0f) return "less than 1 second";
		
		if (elapsedTimeSec < 60.0f) return new String(new Float(elapsedTimeSec).intValue() + " seconds");
		
		float elapsedTimeMin = diff/(60*1000F);
		
		if (elapsedTimeMin < 2.0f) return new String(new Float(elapsedTimeMin).intValue() + " minute");
		
		if (elapsedTimeMin < 59.9f) return new String(new Float(elapsedTimeMin).intValue() + " minutes");
		
		float elapsedTimeHour = diff/(60*60*1000F);
		
		if (elapsedTimeHour < 2.0f) return new String(new Float(elapsedTimeHour).intValue() + " hour");
		
		if (elapsedTimeHour < 24.0f) return new String(new Float(elapsedTimeHour).intValue() + " hours");
		
		return new String(new Float(diff/(24*60*60*1000F)).intValue() + " days");
	}

	public void updateBuffer(String operation) {
		
		if ("/notToDoBuffer".equals(operation)) {
				setDoneDate();
				setBuffer(BufferName.NOTTODO);
			} else {
				if ("/nextBuffer".equals(operation)) {
					setBuffer(getNextBuffer());					
				} else {
					setBuffer(getPreviousBuffer());
				}
			}
			
	    //return getBuffer();
	}

	private String getPreviousBuffer() {

		if (BufferName.OUTPUTQUEUE.equals(this.buffer)) return BufferName.ADJUST;
		
		if (BufferName.ADJUST.equals(this.buffer)) return BufferName.EXPERIMENT;
		
		if (BufferName.EXPERIMENT.equals(this.buffer)) return BufferName.DESIGN;
		
		if (BufferName.DESIGN.equals(this.buffer)) return BufferName.INVESTIGATE;
		
		//if (BufferName.INVESTIGATE.equals(this.buffer)) return BufferName.INPUTQUEUE;
		
		return BufferName.INPUTQUEUE;
	}

	private String getNextBuffer() {
		
		if (BufferName.INPUTQUEUE.equals(this.buffer)) return BufferName.INVESTIGATE;
		
		if (BufferName.INVESTIGATE.equals(this.buffer)) return BufferName.DESIGN;
		
		if (BufferName.DESIGN.equals(this.buffer)) return BufferName.EXPERIMENT;
		
		if (BufferName.EXPERIMENT.equals(this.buffer)) return BufferName.ADJUST;
		
		//if (BufferName.ADJUST.equals(this.buffer)) return BufferName.OUTPUTQUEUE;
		setDoneDate();
		
		return BufferName.OUTPUTQUEUE;
	}

	public void setDoneDate() {
		doneDate = new Date();
	}	 
}
